$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyschool/list',
        datatype: "json",
        colModel: [			
			{ label: 'Id', name: 'schoolId', index: 'school_id', width: 50, key: true },
			{ label: '学校名字', name: 'tname', index: 'tname', width: 80 },
			{ label: '校长', name: 'principalId', index: 'principal_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var principalname = '' ;
                    if(rowdata.principalId!=null&&rowdata.principalId!=""){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyprincipal/info/"+rowdata.principalId,
                            success: function(r){
                                principalname = r.fyPrincipal.tname;

                            }
                        });
					}

                    return principalname;
                } },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            schoolName:null
        },
		showList: true,
		title: null,
		fySchool: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fySchool = {};
			vm.getPrincipal("add");
		},
		update: function (event) {
			var schoolId = getSelectedRow();
			if(schoolId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getPrincipal("update");
            vm.getInfo(schoolId)
		},
		saveOrUpdate: function (event) {
			var url = vm.fySchool.schoolId == null ? "sys/fyschool/save" : "sys/fyschool/update";
			if(vm.fySchool.tname ==''|| typeof vm.fySchool.tname =='undefined'){
                alert("学校名字不能为空");
			}else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.fySchool),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

		},
		del: function (event) {
			var schoolIds = getSelectedRows();
			if(schoolIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyschool/delete",
                    contentType: "application/json",
				    data: JSON.stringify(schoolIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(schoolId){
			$.get(baseURL + "sys/fyschool/info/"+schoolId, function(r){
                vm.fySchool = r.fySchool;
            });
		},
        getPrincipal: function(mn){
            $("#principalId").html("");
            $.get(baseURL + "sys/fyprincipal/alllist", function(r){
                var temp;
                $.each(r.alllist, function (i) {
                    if(mn =='add' && i ==0){
                        vm.fySchool.principalId = r.alllist[i].id;
                    }
                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].tname+"</option>";

                });
                $("#principalId").append(temp);

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'schoolName': vm.q.schoolName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});